package com.house.business.service.impl;

import com.google.common.collect.Lists;
import com.house.business.entity.Message;
import com.house.business.vo.MessageVo;
import com.house.business.mapper.MessageMapper;
import com.house.business.service.MessageService;
import com.house.framework.entity.AjaxResult;
import com.house.framework.entity.User;
import com.house.framework.mapper.UserMapper;
import com.house.framework.service.impl.ServiceBaseImpl;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.house.framework.annotations.Slave;
import com.house.common.IdBuilder;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 留言表 服务实现类
 * </p>
 *
 * @since 2023-05-09
 */
@Service
public class MessageServiceImpl extends ServiceBaseImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    /**
    * 新增留言表
    * @param entity
    * t_message新增数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object addMessage(Message entity){
        entity.setMessageTime(new Date());
        messageMapper.insert(entity);
        return AjaxResult.success();
    }

    /**
    * 修改留言表
    * @param entity
    * t_message修改数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object updateMessage(Message entity){
        entity.setState(1);
        entity.setReplyTime(new Date());
        messageMapper.updateById(entity);
        return AjaxResult.success();
    }

    /**
    * 取指定ID的留言表数据
    * @param id
    * t_message 主键ID
    * @return MessageVo
    * 返回指定ID的留言表数据
    */
    @Slave
    @Override
    public MessageVo getMessageById(Long id) {
        return messageMapper.get(id);
    }

    /**
    * 留言表分页取数
    * @param entityVo
    * t_message 分页取数参数，pageIndex=页码；pageSize=每页记录数；
    * @return
    * 返回分页数据
    */
    @Slave
    @Override
    public IPage<MessageVo> getMessageList(MessageVo entityVo) {
        int pageIndex = entityVo.getPageIndex();
        int pageSize = entityVo.getPageSize();
        IPage page = new Page<MessageVo>(pageIndex, pageSize);
        QueryWrapper<MessageVo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(Objects.nonNull(entityVo.getState()), "t.state", entityVo.getState())
                .like(StringUtils.isNotEmpty(entityVo.getMessageUserName()), "a.name", entityVo.getMessageUserName())
                .orderByDesc(Lists.newArrayList("message_time", "reply_time"));
        IPage iPage = messageMapper.page(page, queryWrapper);
        List<MessageVo> records = iPage.getRecords();
        records.stream().forEach(s -> {
            if (Objects.nonNull(s.getReplyUserId())) {
                User user = userMapper.selectById(s.getReplyUserId());
                if (Objects.nonNull(user)) {
                    s.setReplyUserName(user.getName());
                }
            }
        });
        iPage.setRecords(records);
        return iPage;
    }

    /**
    * 删除指定ID的留言表数据
    * @param id
    * t_message 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteMessageById(Long id){
        messageMapper.delete(id);
    }

    /**
    * 逻辑删除指定ID的留言表数据
    * @param id
    * t_message 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void logicDeleteById(Long id){
        this.logicDelete(id, MessageMapper.class, Message.class);
    }


    /**
    * 批量删除指定ID的留言表数据
    * @param ids
    * t_message 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchDelete(Long[] ids){
        messageMapper.batchDelete(ids);
    }

    /**
    * 批量逻辑删除指定ID的留言表数据
    * @param ids
    * t_message 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchLogicDelete(Long[] ids){
        for(Long id : ids){
            this.logicDelete(id, MessageMapper.class, Message.class);
        }
    }
}
